package guitest;

public class MemoryWindow extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	javax.swing.JTextField memoryField = null;
	javax.swing.JList memoryList = null;
	javax.swing.JPanel memoryPanel = null;
	
	core.ProcessorClass processor = null;

	public MemoryWindow(core.ProcessorClass p){
		this.processor = p;
		this.init();
	}
	protected void prepare_exit(){
		//Operations to prepare exit of the program
		System.exit(0);
	}
	private void init(){
		this.setSize(new java.awt.Dimension(150,250));
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("finestra tancada");
				prepare_exit();
			}
		});
		this.setTitle("SEP16 Simulator");
		this.setContentPane(this.getMemoryPanel());
		this.setVisible(true);
	}
	//Memory Panel
	
	short lastOffset = 0x0000;
	short nValues = 30;
	private void reloadListValues(){
		this.memoryList.setListData(this.processor.mem.getMemoryVector(lastOffset, nValues));
		this.memoryField.setText(java.lang.String.format("0x%04X", this.lastOffset));
	}
	private void changeListValues(){
		short newOffset = 0;
		try{
			newOffset = java.lang.Integer.decode(this.memoryField.getText()).shortValue();
			this.lastOffset = newOffset;
			this.reloadListValues();
		}catch(java.lang.NumberFormatException e){
		}
	}
	private javax.swing.JComponent getMemoryField(){
		this.memoryField = new javax.swing.JTextField("0x0000");
		this.memoryField.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				changeListValues();
			}
		});
		return(this.memoryField);
	}

	private javax.swing.JComponent getMemoryList(){
		this.memoryList = new javax.swing.JList();
		this.memoryList.setCellRenderer(new guitest.MemoryWindow.MemoryRender());
		return(this.memoryList);
	}
	private javax.swing.JPanel getMemoryPanel(){
		this.memoryPanel = new javax.swing.JPanel(new java.awt.BorderLayout());
		this.memoryPanel.add(this.getMemoryField(), java.awt.BorderLayout.NORTH);
		this.memoryPanel.add(this.getMemoryList(), java.awt.BorderLayout.CENTER);
		return(this.memoryPanel);
	}

	class MemoryRender extends javax.swing.JPanel implements javax.swing.ListCellRenderer{
		static final long serialVersionUID = 0;
		javax.swing.JTextField t = new javax.swing.JTextField();
		javax.swing.JTextField pos = new javax.swing.JTextField();

		java.lang.Short memValue = null;
		java.lang.Short posValue = null;
		public java.awt.Component getListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean selected, boolean focused) {
			//Extremely dirty trick for java: two shorts are encoded into a Integer
			java.lang.Integer aux = (java.lang.Integer) value;
			memValue = (short)(aux.intValue() & 0x0000FFFF);
			posValue = (short)((aux.intValue() & 0xFFFF0000)>>16);

			//this.setLayout(new java.awt.GridLayout(0,2));
			this.setLayout(new java.awt.BorderLayout());
			t.setText(String.format("0x%04X", memValue.shortValue()));
			t.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
			t.setBorder(null);
			pos.setBorder(null);
			pos.setText(String.format("0x%04X", posValue.shortValue()));
			pos.setBackground(java.awt.Color.lightGray);
			pos.setSize(pos.getWidth() + 40, pos.getHeight());
			this.add(pos, java.awt.BorderLayout.WEST);
			this.add(t, java.awt.BorderLayout.CENTER);
			return(this);
		}
	}

	public static void main(String args[]){
		core.ProcessorClass p = new core.ProcessorClass();
		guitest.MemoryWindow mw =	new MemoryWindow(p);
		p.loadFile(new java.io.File("prog1.s16"));
		p.run();
		mw.memoryList.setListData(p.mem.getMemoryVector());
	}
}
